﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using HA.Data;

using System.Windows.Controls;

namespace HA.ModFlow.Application
{
   public class Grid3dViewModel:MDTVIViewModel
    {
       public Grid3dViewModel(MDTVIViewModel parent,Grid3d data):base(parent,false)
       {
           this._gridData = data;

           this._contextMenu = new System.Windows.Controls.ContextMenu();

           MenuItem mi = new MenuItem();
           mi.Header = "缩放到...";
           mi.Command = GISCommands.ExtentToCurrent;

           mi.CommandParameter = GisModule.DefaultModule.GridLayerId;

           this._contextMenu.Items.Add(mi);

           this._contextMenu.Items.Add(new Separator());

           mi = new MenuItem();
           mi.Header = "新建MODFLOW";
           mi.Command = DataCommands.AddModflow;

           mi.CommandParameter = this._gridData;

           this._contextMenu.Items.Add(mi);


           mi = new MenuItem();
           mi.Header = "新建 MT3D...";
           mi.Command = DataCommands.AddMT3DMS;

           this._contextMenu.Items.Add(mi);




           mi = new MenuItem();
           mi.Header = "删除网格";
           mi.Command = DataCommands.RemoveGrid3D;
           mi.CommandParameter = data;

           this._contextMenu.Items.Add(mi);

           //mi = new MenuItem();
           //mi.Header = "重命名";  
           //this._contextMenu.Items.Add(mi);

           Separator sep = new Separator();
           this._contextMenu.Items.Add(sep);

           mi = new MenuItem();
           mi.Header = "属性";
           mi.Command = Data2Commands.Grid3dProp;
           mi.CommandParameter = this._gridData;

           this._contextMenu.Items.Add(mi);





           //mi = new MenuItem();
           //mi.Header = "Show MODPATH Menu";
           //this._contextMenu.Items.Add(mi);

           //mi = new MenuItem();
           //mi.Header = "New MT3D...";
           //this._contextMenu.Items.Add(mi);

           //mi = new MenuItem();
           //mi.Header = "New Material Zones...";
           //this._contextMenu.Items.Add(mi);

           //Separator sep1 = new Separator();
           //this._contextMenu.Items.Add(sep1);

           //mi = new MenuItem();
           //mi.Header = "Isolate";
           //this._contextMenu.Items.Add(mi);

           //Separator sep2 = new Separator();
           //this._contextMenu.Items.Add(sep2);

           //mi = new MenuItem();
           //mi.Header = "Convert To";
           ////子项
           //MenuItem menu = new MenuItem();
           //menu.Header = "3D Scatter points";
           //mi.Items.Add(menu);

           //menu = new MenuItem();
           //menu.Header = "3D Mesh";
           //mi.Items.Add(menu);

           //menu = new MenuItem();
           //menu.Header = "2D Grid";
           //mi.Items.Add(menu);
           ////子荐结束
           //this._contextMenu.Items.Add(mi);


           this._imageSource = VMImageSource.Grid3d;



           this.LoadChildren();

       }




       Grid3d _gridData;

       public Grid3d GridData
       {
           get { return _gridData; }
         
       }

       public override System.Windows.Input.ICommand DoubleClickCommand
       {
           get
           {
               return Data2Commands.Grid3dProp;
           }
       }



       public override string Desc
       {
           get
           {
               return "三维网格";
           }
       }


       protected override void LoadChildren()
       {

           //if (ProjectSerivce.DefaultService.Project.MfData != null)
           //{
           //    ModFlowViewModel mdvm = new ModFlowViewModel(this, ProjectSerivce.DefaultService.Project.MfData);
           //    this.Children.Add(mdvm);
           //    foreach (DSGrid3dCollecion item in this._gridData.DatasetCollecion)
           //    {
           //        DSGrid3dCollecionViewModel vm = new DSGrid3dCollecionViewModel(this, item);
           //        this.Children.Add(vm);
           //    }
           //    //这里加入结果的显示程序，用于暂时测试
           //    //MFResultViewModel mresult = new MFResultViewModel(this, ProjectSerivce.DefaultService.Project.MfData.MfResult);
           //    //base.Children.Add(mresult);    
           //}

           if (ProjectService.DefaultService.Project.MfData != null)
           {
               //添加modflow项目的目录

               ModFlowViewModel mdvm = new ModFlowViewModel(this, ProjectService.DefaultService.Project.MfData);

               this.Children.Add(mdvm);

           }
           //添加所有的结果  即使对应的项目不存在了 仍然显示结果数据
           if (this._gridData != null && this._gridData.DatasetCollecion != null)
           {
               foreach (DSGrid3dCollecion item in this._gridData.DatasetCollecion)
               {

                   DSGrid3dCollecionViewModel vm = new DSGrid3dCollecionViewModel(this, item);

                   this.Children.Add(vm);

                   #region 未使用

                   //遍历所有存储的结果数据
                   //if (item.Key .ToString ()=="ModflowResult")
                   {
                       //如果是Modflow的结果数据
                       //if (ProjectSerivce.DefaultService.Project.MfData != null)
                       //{
                       //    //添加modflow的目录
                       //    ModFlowViewModel mdvm = new ModFlowViewModel(this, ProjectSerivce.DefaultService.Project.MfData);
                       //    this.Children.Add(mdvm);
                       //}
                       //添加modflow的结果目录
                       //DSGrid3dCollecionViewModel vm = new DSGrid3dCollecionViewModel(this, item);
                       //this.Children.Add(vm);

                   }

                   if (item.Key.ToString() == "MT3DMSResult")
                   {
                       //如果是MT3d的结果数据
                       //if (ProjectSerivce.DefaultService.Project.Mt3dmsData  != null)
                       //{
                       //    //添加MT3d的目录   原设计是在  ModFlowViewModel  中完成的  此处不再进行处理
                       //    //ModFlowViewModel mdvm = new ModFlowViewModel(this, ProjectSerivce.DefaultService.Project.MfData);
                       //    //this.Children.Add(mdvm);
                       //}
                       ////添加MT3d的结果目录
                       //DSGrid3dCollecionViewModel vm = new DSGrid3dCollecionViewModel(this, item);
                       //this.Children.Add(vm);

                   }

                   if (item.Key.ToString() == "OtherDataSetResult")
                   {
                       //如果是其他的结果数据 例如：二维离散点

                       //此处按照一直显示处理  不管是否仍然有二维离散点数据

                       //if (ProjectSerivce.DefaultService.Project.Scatter2dData != null && ProjectSerivce.DefaultService.Project.Scatter2dData.Datas != null && ProjectSerivce.DefaultService.Project.Scatter2dData.Datas.Count > 0)
                       //{
                       //添加结果目录
                       //DSGrid3dCollecionViewModel vm = new DSGrid3dCollecionViewModel(this, item);
                       //this.Children.Add(vm);

                       //}

                   }

                   #endregion

               }

           }






         



           ProjectService.DefaultService.Project.ModFlowChanged += new EventHandler<ModelChangedEventArgs>(Project_ModFlowChanged);

           this._gridData.DataSetCollectionChanged += new EventHandler<CollecionChangedEventArgs>(_gridData_DataSetCollectionChanged);

          

       }

       void _gridData_DataSetCollectionChanged(object sender, CollecionChangedEventArgs e)
       {
           switch (e.Type)
           {
               case CollectionChangedType.ADD:

                   DSGrid3dCollecion grid=(DSGrid3dCollecion)e.Child;
                   DSGrid3dCollecionViewModel vm = new DSGrid3dCollecionViewModel(this,grid);

                   this.Children.Insert(0,vm);

                   //this.Children.Add(vm);

                   break;
               case CollectionChangedType.REMOVE:

                   DSGrid3dCollecion c = (DSGrid3dCollecion)e.Child;
                   DSGrid3dCollecionViewModel v = this.GetDSCollecion(c);

                   if (v!=null)
                   {
                       this.Children.Remove(v);
                       
                   }


                   UIModule.DefaultModule.ShowTimeStepView(false);


                   break;
               case CollectionChangedType.MOVE:
                   break;
               case CollectionChangedType.REPLACE:



                   break;
               case CollectionChangedType.ITEMEDIT:
                   break;
               case CollectionChangedType.RESET:

                   this.Children .Clear ();

                   foreach (DSGrid3dCollecion item in this._gridData.DatasetCollecion)
                   {
                       DSGrid3dCollecionViewModel vms = new DSGrid3dCollecionViewModel(this, item);

                       this.Children.Add(vms);


                   }

                   UIModule.DefaultModule.ShowTimeStepView(false);

                   break;
               default:
                   break;
           }
       }

       void Project_ModFlowChanged(object sender, ModelChangedEventArgs e)
       {
           switch (e.ChangedType)
           {
               case ModelChangedType.CRREATED:

                   
               ModFlowViewModel mdvm = new ModFlowViewModel(this, ProjectService.DefaultService.Project.MfData);

               this.Children.Add(mdvm);



                   break;
               case ModelChangedType.DELETED:

                   ModFlowViewModel md = (ModFlowViewModel)this.GetModflow();

                   if (md!=null)
                   {
                       this.Children.Remove(md);
                       
                   }

                


                   break;
               default:
                   break;
           }
       }


       public ModFlowViewModel GetModflow()
       {
           foreach (MDTVIViewModel item in this.Children)
           {
               if (item is ModFlowViewModel)
               {

                   return (ModFlowViewModel)item;
                   
               }
               
           }

           return null;

       }

       public DSGrid3dCollecionViewModel GetDSCollecion(DSGrid3dCollecion collecion)
       {
           foreach (MDTVIViewModel item in this.Children)
           {
               if (item is DSGrid3dCollecionViewModel)
               {

                   DSGrid3dCollecionViewModel vm = (DSGrid3dCollecionViewModel)item;

                   if (vm.Collecion==collecion)
                   {
                       return vm;
                       
                   }


                

               }

           }

           return null;
 
       }



       protected override void OnCheckedChanged()
       {

           ///注意这里指的是包含了源汇等所有项的可见性

             string name = GisModule.DefaultModule.GridGroupLayerId;
             GisModule.DefaultModule.SetLayerVisible(name, this.IsChecked);


           //等值线 



             string contourName = GisModule.DefaultModule.IsolineLayerId;

             GisModule.DefaultModule.SetLayerVisible(contourName, this.IsChecked);

       }




       protected override void OnSelectedChanged()
       {
           if (this.IsSelected)
           {
              

               if (ProjectService.DefaultService.Project.Grid3dData.MainGrid != null)
               {
                   string name = GisModule.DefaultModule.GridLayerId;

                   GisModule.DefaultModule.SetCurrentLayer(name);

                   //GisModule.DefaultModule.GisManager.InteractiveSystem.CurrentLayer = GisModule.DefaultModule.GisManager.LayerSystem.LayerCollection.GetLayer(ProjectSerivce.DefaultService.Project.Grid3dData.MainGrid.Key.ToString());



               }

               UIModule.DefaultModule.RibbonV2ViewModel.ViewOpViewModel.ToolVM.CurrentTool = APPTOOLMODE.GRID3D;

           }

         
       }


    }
}
